﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;
using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string ProductSale_M_Add(ProductSale_M model, int ACID);

        [OperationContract]
        string ProductSale_M_Update(ProductSale_M model, int ACID);

        [OperationContract]
        string ProductSale_MBydid_Delete(string djn, int ACID);

        [OperationContract]
        ProductSale_M ProductSale_MBydid_Get(int did, int ACID);

        [OperationContract]
        List<ProductSale_M> ProductSale_M_Get(string sortBy, int ACID);

        /// <summary>
        /// 查询销售单据列表-视图
        /// </summary>
        /// <param name="sortBy"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<View_ProductSale_M> ViewProductSale_M(string sortBy, int ACID);

        [OperationContract]
        ProductSale_M GetPrductSale_M_ByID(string djn, int ACID);


    }
    public partial class Service : IService
    {
        #region 添加表ProductSale_M数据

        public string ProductSale_M_Add(ProductSale_M model, int ACID)
        {
            if (model.clientID == null || model.clientID.Equals(Guid.Empty))
            {
                return "『客户』不能为空！";
            }
            if (model.warehouseID == null || model.warehouseID.Equals(Guid.Empty))
            {
                return "『仓库』不能为空！";
            }
            if (model.odate == null)
            {
                return "『日期』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductSale_M.Count(p => p.djn == model.djn) > 0)
                    {
                        return "主键已存在！";
                    }
                    ctx.ProductSale_M.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }

        #endregion

        #region 添加ProductSale_M数据 事物用重构
        /// <summary>
        /// 添加KCSale_M事物用重构
        /// </summary>
        /// <param name="model">KCSale_M实体</param>
        /// <returns>空或错误</returns>
        private string ProductSale_M_Add(ProductSale_M model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (model.clientID == null || model.clientID.Equals(Guid.Empty))
            {
                return "『客户』不能为空！";
            }
            if (model.warehouseID == null || model.warehouseID.Equals(Guid.Empty))
            {
                return "『仓库』不能为空！";
            }
            if (model.odate == null)
            {
                return "『日期』不能为空！";
            }
            if (ctx.ProductSale_M.Count(p => p.djn == model.djn) > 0)
            {
                return "主键已存在！";
            }
            ctx.ProductSale_M.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion

        #region 修改销售单据表里ProductSale_M的数据

        public string ProductSale_M_Update(ProductSale_M model, int ACID)
        {
            if (model.clientID == null || model.clientID.Equals(Guid.Empty))
            {
                return "『客户』不能为空！";
            }
            if (model.warehouseID == null || model.warehouseID.Equals(Guid.Empty))
            {
                return "『仓库』不能为空！";
            }
            if (model.odate == null)
            {
                return "『日期』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductSale_M.Count(p => p.djn == model.djn) != 1)
                    {
                        return "未找到指定记录或记录不唯一！";
                    }
                    ProductSale_M pmodel = ctx.ProductSale_M.Single(p => p.djn == model.djn);
                    pmodel.clientID = model.clientID;
                    pmodel.warehouseID = model.warehouseID;
                    pmodel.odate = model.odate;
                    pmodel.linkman = model.linkman;
                    pmodel.linktel = model.linktel;
                    pmodel.linkaddress = model.linkaddress;
                    pmodel.srpznum = model.srpznum;
                    pmodel.fpnum = model.fpnum;
                    pmodel.cbpznum = model.cbpznum;
                    pmodel.cnnum = model.cnnum;
                    pmodel.remarks = model.remarks;
                    pmodel.jsuser = model.jsuser;

                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 修改失败!错误详细信息：" + ex.Message;
                }
            }
        }

        #endregion

        #region 修改销售单据表里ProductSale_M的数据事物重构

        public string ProductSale_M_Update(ProductSale_M model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (model.clientID == null || model.clientID.Equals(Guid.Empty))
            {
                return "『客户』不能为空！";
            }
            if (model.warehouseID == null || model.warehouseID.Equals(Guid.Empty))
            {
                return "『仓库』不能为空！";
            }
            if (model.odate == null)
            {
                return "『日期』不能为空！";
            }

            try
            {
                if (ctx.ProductSale_M.Count(p => p.djn == model.djn) != 1)
                {
                    return "未找到指定记录或记录不唯一！";
                }
                ProductSale_M pmodel = ctx.ProductSale_M.Single(p => p.djn == model.djn);
                pmodel.djn = model.djn;
                pmodel.clientID = model.clientID;
                pmodel.warehouseID = model.warehouseID;
                pmodel.odate = model.odate;
                pmodel.linkman = model.linkman;
                pmodel.linktel = model.linktel;
                pmodel.linkaddress = model.linkaddress;
                pmodel.srpznum = model.srpznum;
                pmodel.fpnum = model.fpnum;
                pmodel.cbpznum = model.cbpznum;
                pmodel.cnnum = model.cnnum;
                pmodel.remarks = model.remarks;
                pmodel.jsuser = model.jsuser;
                pmodel.ischeck = model.ischeck;
                pmodel.isfinish = model.isfinish;
                pmodel.checkstatus = model.checkstatus;
                pmodel.total = model.total;
                pmodel.banlance = model.banlance;
                pmodel.discount = model.discount;

                return string.Empty;
            }
            catch (Exception ex)
            {
                return " 修改失败!错误详细信息：" + ex.Message;
            }

        }

        #endregion

        #region 删除ProductSale_M表中的数据

        public string ProductSale_MBydid_Delete(string djn, int ACID)
        {
            if (string.IsNullOrEmpty(djn))
            {
                return "『单据号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductSale_M.Count(p => p.djn == djn) != 1)
                    {
                        return null;
                    }
                    ProductSale_M model = ctx.ProductSale_M.Single(p => p.djn == djn);
                    if (model.ysmoney > 0)
                    {
                        return "该单据已收款，不能删除！";
                    }
                    ctx.ProductSale_M.DeleteOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 删除失败!错误详细信息：" + ex.Message;
                }
            }
        }

        //事务执行重构
        public string ProductSale_MBydid_Delete(string djn, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(djn))
            {
                return "『单据号』不能为空！";
            }
            if (ctx.ProductSale_M.Count(p => p.djn == djn) != 1)
            {
                return null;
            }
            ProductSale_M model = ctx.ProductSale_M.Single(p => p.djn == djn);
            if (model.ysmoney > 0)
            {
                return "该单据已收款，不能删除！";
            }
            ctx.ProductSale_M.DeleteOnSubmit(model);
            return string.Empty;
        }

        #endregion

        public ProductSale_M ProductSale_MBydid_Get(int did, int ACID)
        {
            throw new NotImplementedException();
        }

        #region  获取ProductSale_M表数据
        /// <summary>
        /// 获取ProductSale_M
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<ProductSale_M></returns>
        public List<ProductSale_M> ProductSale_M_Get(Expression<Func<ProductSale_M, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.ProductSale_M.ToList();
                        }
                        else
                        {
                            return ctx.ProductSale_M.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.ProductSale_M.Where(predicate).ToList();
                        }
                        else
                        {
                            return ctx.ProductSale_M.Where(predicate).OrderBy(sortBy).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion



        public List<ProductSale_M> ProductSale_M_Get(string sortBy, int ACID)
        {
            throw new NotImplementedException();
        }


        #region 查询单据列表，无条件


        public List<View_ProductSale_M> ViewProductSale_M(string sortBy, int ACID)
        {
            return ViewProductSale_M(null, sortBy, ACID);
        }
        #endregion

        #region 重构查询销售单据视图列表

        public List<View_ProductSale_M> ViewProductSale_M(Expression<Func<View_ProductSale_M, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null) //查询条件为空
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.View_ProductSale_M.ToList();
                        }
                        else
                        {
                            return ctx.View_ProductSale_M.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.View_ProductSale_M.Where(predicate).ToList();
                        }
                        else
                        {
                            return ctx.View_ProductSale_M.Where(predicate).OrderBy(sortBy).ToList();

                        }
                    }
                }
                catch
                {
                    return null;
                }

            }
        }

        #endregion


        public ProductSale_M GetPrductSale_M_ByID(string djn, int ACID)
        {
            if (string.IsNullOrEmpty(djn))
            {
                return null;
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductSale_M.Count(p => p.djn == djn) != 1)
                    {
                        return null;
                    }
                    return ctx.ProductSale_M.SingleOrDefault(p => p.djn == djn);
                }
                catch
                {
                    return null;
                }
            }

        }
    }
}
